// pages/usercenter/addresslist/addresslist.js
var utils = require('../../../utils/util');

Page({
  gotoPage: utils.gotoPage,

  /**
   * 页面的初始数据
   */
  data: {
    selected_addressidx: '-1',
    addressList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var HOST = getApp().globalData.HOST;
    var that = this;
    var openid = wx.getStorageSync('openid');

    // 获取地址列表
    wx.request({
      url: HOST + 'user/address',
      data: {
        // ACTION: 'QUERY',
        openid: openid
      },
      success: function(resp) {
        console.log('address list:', resp)
        // set data        
        that.setData({
          addressList: resp.data.data
        });
      },
    })

  },

  // 编辑地址
  editAddress: function(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/usercenter/addressnew/addressnew?id=' + id,
    })
  },

  setDefaultAddress: function(event) {
    console.log('radio change:', event.detail.value);

    var address = this.data.addressList[event.detail.value];
    wx.setStorageSync('address', address);

    this.setData({
      selected_addressidx: event.detail.value
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '国学小书童',
      path: '/pages/index/index',
      imageUrl: '/images/g1.jpg',
    }
  }
})